﻿(function (window) {

    //图表对象
    var chart = {};

    /*
        精确到两位小数
        参数:
          --原数字
    */
    function toDecimal2(x) {
        var f = parseFloat(x);
        if (isNaN(f))
        {
            return false;
        }
        var f = Math.round(x * 100) / 100;
        var s = f.toString();
        var rs = s.indexOf('.');
        if (rs < 0)
        {
            rs = s.length;
            s += '.';
        }
        while (s.length <= rs + 2)
        {
            s += '0';
        }
        return s;
    }

    //饼图
    chart.pieChart = (function (title, interval, fontColor) {
        var ctx = null;
        var haveInterval = interval;
        this.title = title || "";
        fontColor = fontColor || "#000";

        /*
            添加数据
            参数:
              --名称
              --数据数字
              --颜色
        */
        this.addData = function (name, number, dataColor) {
            names.add(name);
            color.add(dataColor);
            data.add(number);
        };

        //数据
        var names = [];
        var color = [];
        var data = [];
        var list = new Array(data.length);

        /*
            获取扇形
        */
        function getFanShape(x0, y0) {
            var angle_Start = 0;
            //圆心坐标
            var rx = 100;
            var ry = 100;
            var x = 0;
            var y = 0;
            //x1,y1为弧的起点
            for (var i = 0; i < data.length; i++)
            {
                if (haveInterval)
                {
                    rx = x0;
                    ry = y0;
                }
                else
                {
                    rx = ry = 100;
                }
                var x1 = 0;
                var y1 = 0;
                if (x == 0 && y == 0)
                {
                    x1 = rx + 100 * (Math.cos(angle_Start * Math.PI / 180));
                    y1 = ry + 100 * (Math.sin(angle_Start * Math.PI / 180));
                }
                else
                {
                    x1 = x;
                    y1 = y;
                }
                //x3,y3 为弧上中点位置的坐标
                var x3 = rx + 100 * (Math.cos(((angle_Start + Math.PI * (data[i] / 100)) + Math.PI * 2 * (data[i] / 100) * Math.PI / 180)));
                var y3 = ry + 100 * (Math.sin(((angle_Start + Math.PI * (data[i] / 100)) + Math.PI * 2 * (data[i] / 100) * Math.PI / 180)));

                angle_Start += Math.PI * 2 * (data[i] / 100);
                //x2,y2为弧的终点
                var x2 = rx + 100 * (Math.cos((angle_Start + Math.PI * 2 * (data[i] / 100) * Math.PI / 180)));
                var y2 = ry + 100 * (Math.sin((angle_Start + Math.PI * 2 * (data[i] / 100) * Math.PI / 180)));
                x = x2;
                y = y2;
                var FanShape = {};
                FanShape.Id = i;
                FanShape.x1 = x1;
                FanShape.y1 = y1;
                FanShape.x2 = x2;
                FanShape.y2 = y2;
                FanShape.x3 = x3;
                FanShape.y3 = y3;
                FanShape.x = rx;
                FanShape.y = ry;
                FanShape.data = data[i];
                //半径
                FanShape.r = 100;
                list[i] = FanShape;
            }
        }

        /*
            获取饼图
        */
        function pieMap() {
            var sb = spriteBatch;
            sb.begin();
            ctx.translate(100, 100);

            //画弧度
            var angle_Start = 0;
            for (var i = 0; i < data.length; i++)
            {
                ctx.beginPath();
                if (haveInterval)
                {
                    setShadow(true);
                    var sh = list[i];
                    var x = (sh.x1 + sh.x2) / 2;
                    var y = (sh.y1 + sh.y2) / 2;
                    x = (100 + x) / 2;
                    y = (100 + y) / 2;

                    x = (100 + x) / 2;
                    y = (100 + y) / 2;
                    getFanShape(x, y);
                    ctx.moveTo(x, y);
                    ctx.arc(x, y, 100, angle_Start, angle_Start + Math.PI * 2 * (data[i] / 100), false);
                }
                else
                {
                    setShadow(false);
                    ctx.moveTo(100, 100);
                    ctx.arc(100, 100, 100, angle_Start, angle_Start + Math.PI * 2 * (data[i] / 100), false);
                }
                ctx.fillStyle = color[i];
                ctx.fill();
                if (!haveInterval)
                {
                    ctx.strokeStyle = "#fff";
                    ctx.stroke();
                }
                angle_Start += Math.PI * 2 * (data[i] / 100);
                writeLine(i);
            }
            sb.end();
        }

        /*
            扇形外画线
        */
        function writeLine(i) {
            var sb = spriteBatch;
            setShadow(true);
            var s = list[i];
            var p = {
                x: (3 * s.x3 - s.x) / 2,
                y: (3 * s.y3 - s.y) / 2
            };
            sb.drawLine(s.x3, s.y3, p.x, p.y, fontColor);
            sb.fillText(s.data + "%", p.x - 15, p.y + 5, fontColor, "Bold 16px Arial");
        }

        /*
            获取图表图像
        */
        this.getMap = function () {
            var canvasCtx = graphics.getContext();
            var c = document.createElement("canvas");
            c.width = 600;
            c.height = 500;
            ctx = c.getContext("2d");
            graphics.setContext(ctx);

            var sb = spriteBatch;

            var sum = 0;
            for (var i = data.length; i--;)
            {
                sum += data[i];
            }
            var reSum = 0;
            for (var i = data.length; i--;)
            {
                data[i] = toDecimal2((data[i] / sum * 100));
            }


            ctx.font = "Bold 24px Arial";

            setShadow(true);
            if (!haveInterval)
            {
                sb.fillCircle(198, 198, 100, "#fff");
            }

            getFanShape(100, 100);
            pieMap();

            setShadow(true);
            for (var i = 0; i < list.length; i++)
            {
                if (names[i])
                {
                    sb.fillRect(450, i * 30 + 30, 10, 10, color[i]);
                    sb.fillText(names[i], 470, i * 30 + 45, fontColor, "Bold 20px Arial");
                }
            }
            sb.fillText(this.title, 600 / 2 - ctx.measureText(this.title).width / 2, 450, fontColor, "24px 宋体");

            graphics.setContext(canvasCtx);

            return c;
        }

        /*
            设置阴影
            参数:
              --是否显示
        */
        function setShadow(isShow) {
            if (isShow)
            {
                spriteBatch.setShadow(5, 5, 5, "#bbb");
            }
            else
            {
                spriteBatch.setShadow();
            }
        }
    });

    ////柱形图
    //var barChart = (function (title, fontColor) {
    //    //设置图表画布
    //    var canvas = document.createElement("canvas");
    //    c.width = 600;
    //    c.height = 500;
    //    var ctx = canvas.getContext("2d");

    //    //设置图表属性
    //    this.title = title || "";
    //    fontColor = fontColor || "#000";


    //    /*
    //        获取图表
    //    */
    //    this.getMap = function () {
    //        var canvasCtx = graphics.getContext();
    //        graphics.setContext(ctx);

    //        graphics.setContext(canvasCtx);
    //    };
    //});



    //公开对象
    window.chart = chart;
})(window);